package com.ay277.htqoa.base.dao;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.List;


/**
 * Created by Jun on 2016/12/7.
 */
@Repository
public abstract class BaseDao<E extends Serializable,PK extends  Serializable> extends HibernateDaoSupport {


     protected   Class type;
    @Autowired
    private SessionFactory mySessionFactory;

    @PostConstruct
    public void set(){
        setSessionFactory(mySessionFactory);
    }
    public void save(E entity){
        this.getHibernateTemplate().save(entity);
    }
    public void update(E entity){
        this.getHibernateTemplate().update(entity);
    }
    public void delete(E entity){
        this.getHibernateTemplate().delete(entity);
    }


    public E get(PK id){

        return (E) this.getHibernateTemplate().get(type,id);
    }

    public List<E> list(){

        return (List<E>) this.getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(type));
    }
}
